package com.wqr.mobile.android.ui;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;

/**
 * activity templete
 * 
 * @author wenqiurong
 * 
 */
public abstract class BaseListActivity extends ListActivity implements IAsyncActivity {

	public static String TAG;
	private ProgressDialog progressDialog;

	private boolean destroyed = false;

	/**
	 * Initializa models
	 */
	protected abstract void loadData();

	/**
	 * clear object
	 */
	protected abstract void destory();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		TAG = this.getClass().getSimpleName();
		loadData();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		this.destroyed = true;
		destory();
		Log.i(TAG, "templete onDestroy");
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
			return false;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	@Override
	public void showLoadingProgressDialog() {
		this.showProgressDialog("Loading. Please wait...");
	}

	@Override
	public void showProgressDialog(CharSequence message) {
		if (this.progressDialog == null) {
			this.progressDialog = new ProgressDialog(this);
			this.progressDialog.setIndeterminate(true);
		}

		this.progressDialog.setMessage(message);
		this.progressDialog.show();
	}

	@Override
	public void dismissProgressDialog() {
		if (this.progressDialog != null && !this.destroyed) {
			this.progressDialog.dismiss();
		}
	}
}
